limsEquipment.multiChoosePage = {
    getChooseGridParam : function() {
        var _this = this, columns = limsEquipment.getChooseListColumns();
        _this.param.single ? columns.unshift({
            radio : true
        }) : columns.unshift({
            checkbox : true
        });
        return {
            type : 'grid',
            id : 'equipment-choose-mult-list-grid',
            url : limsEquipment.baseUrl + '/queries',
            requestData : _this.param,
            columns : columns,
            filterOpen : true,
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    _this.add();
                }
            } ],
            onBeforeLoad : function(data) {
                var choosenData = Gikam.getComp('equipment-choosen-mult-list-grid').getData();
                if (!Gikam.isEmptyObject(choosenData)) {
                    data = data.filter(function(item) {
                        var flag = true;
                        Gikam.each(choosenData, function(i, row) {
                            if (row.id === item.id) {
                                flag = false;
                            }
                        });
                        return flag;
                    });
                }
                return data;
            }
        }
    },

    getChoosenGridParam : function() {
        var _this = this, columns = limsEquipment.getChooseListColumns();
        _this.param.single ? columns.unshift({
            radio : true
        }) : columns.unshift({
            checkbox : true
        });
        var choosenData = [];
        if (!Gikam.isEmpty(_this.param.eqIds)) {
            Gikam.postSync(limsEquipment.baseUrl + '/queries',
                Gikam.getJsonWrapper({
                    f : {
                        id_IN : _this.param.eqIds
                    },
                    s: 100
                })).done(function (data) {
                if (!Gikam.isEmpty(data)) {
                    choosenData.push(...data.rows);
                }
            })
        }
        return {
            type : 'grid',
            id : 'equipment-choosen-mult-list-grid',
            data : choosenData,
            toolbar : [ {
                type : 'button',
                text : '移除',
                icon : 'remove',
                onClick : function() {
                    _this.remove();
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.CONFIRM',
                icon : 'select',
                onClick : function() {
                    Gikam.getLastModal().close(Gikam.getComp('equipment-choosen-mult-list-grid').getData());
                }
            } ],
            columns : columns,
            page : false
        }
    },

    add : function() {
        var _this = this;
        var chooseMultListGrid = Gikam.getComp('equipment-choose-mult-list-grid');
        var choosenMultListGrid = Gikam.getComp('equipment-choosen-mult-list-grid');
        var addUserData = [];
        var choosenData = choosenMultListGrid.getData() ? choosenMultListGrid.getData() : [], removedRowIndex = chooseMultListGrid.model.$children[0].checkeds;

        var chooseData = Gikam.jQuery.map(chooseMultListGrid.getData(), function(rowData, index) {
            if (Gikam.jQuery.inArray(index, removedRowIndex) < 0) {
                return rowData;
            }
            addUserData.push(rowData);
        });

        choosenData = choosenData.concat(addUserData);

        _this.arrangeGrid(choosenData);

    },

    remove : function() {

        var _this = this, chooseMultListGrid = Gikam.getComp('equipment-choose-mult-list-grid'), choosenMultListGrid = Gikam.getComp('equipment-choosen-mult-list-grid'), oldChoosenData = choosenMultListGrid.getData(), addUserData = choosenMultListGrid.getSelections(), removedRowIndex = choosenMultListGrid.model.$children[0].checkeds;

        var choosenData = Gikam.jQuery.map(oldChoosenData, function(rowData, index) {
            if (Gikam.jQuery.inArray(index, removedRowIndex) < 0) {
                return rowData;
            }
        });

        _this.arrangeGrid(choosenData);
    },

    arrangeGrid : function(choosenData) {
        var chooseMultListGrid = Gikam.getComp('equipment-choose-mult-list-grid'), choosenMultListGrid = Gikam.getComp('equipment-choosen-mult-list-grid');

        choosenMultListGrid.refreshByData({
            rows : choosenData
        });

        chooseMultListGrid.refresh();

        choosenMultListGrid.selectedRowIndex = [];

    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            renderTo : Gikam.getLastModal().window.$dom,
            west : {
                width : '50%',
                items : [ _this.getChooseGridParam() ]
            },
            center : {
                items : [ _this.getChoosenGridParam() ]
            }
        });
    },

    init : function(param) {
        this.param = param;
        this.create();
    }
};
